package com.jackchong.widget;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * author : jack(黄冲)
 * e-mail : 907755845@qq.com
 * create : 2019/4/17
 * desc   :
 */
public class EventLnearLayout extends JLinearLayout {

    private boolean mIntercept;
    private OnTouchListener mOnTouchListener;

    private OnDownListener mOnDownListener;

    public EventLnearLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                down(ev);
                break;
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    private void down(MotionEvent ev) {
        if (mOnDownListener != null) mOnDownListener.onDown(ev);
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (mIntercept){
            return true;
        }
        return super.onInterceptTouchEvent(ev);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mOnTouchListener != null) mOnTouchListener.onTouch(event);
        return true;
    }


    public interface OnDownListener{
        void onDown(MotionEvent event);
    }

    public interface OnTouchListener{
        void onTouch(MotionEvent event);
    }



    public void setOnDownListener(OnDownListener onDownListener){
        mOnDownListener = onDownListener;
    }

    public void setOnTouchEvent(OnTouchListener onTouchEvent){
        mOnTouchListener = onTouchEvent;
    }

    public void setInterceptEvent(boolean intercept){
        mIntercept = intercept;
    }
}
